﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 线程锁
{
    class Program
    {
        static int i = 2;
        static void Main(string[] args)
        {
            TreadTest t1 = new TreadTest(2,i);
            TreadTest t2 = new TreadTest(2, i);
            Thread[] threads = new Thread[2];
            //threads[0] = new Thread(t1.Hanshu);
            //threads[1] = new Thread(t2.Hanshu);
            //threads[0] = new Thread(t1.Hanshu2);
            //threads[1] = new Thread(t2.Hanshu2);

            threads[0] = new Thread(t1.Hanshu3);
            threads[1] = new Thread(t2.Hanshu3);
            /** 如果在执行锁的时候报错，那么这个锁得不到释放，
             * 如果在执行Monitor.Enter方法的时候失败，
             * 即没有拿到锁，lockTaken就为false，
             * finally语句块中无需释放锁；
             * 如果在Monitor.Enter之后出现异常，因为线程拿到了锁，
             * lockTaken就为true,最后在finally语句块中释放锁。 
            **/


            threads[0].Start();
            threads[1].Start();


            Console.ReadKey();

        }



    }
}
